home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / frasrc19.zip / PLOT3D.C < prev    next >
C/C++ Source or Header  |  1994-11-28  |  14KB  |  434 lines

  1. /*
  2.     This file includes miscellaneous plot functions and logic
  3.     for 3D, used by lorenz.c and line3d.c
  4.     By Tim Wegner and Marc Reinig.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "fractint.h"
  10. #include "fractype.h"
  11. #include "prototyp.h"
  12.  
  13. /* Use these palette indices for red/blue - same on ega/vga */
  14. #define PAL_BLUE    1
  15. #define PAL_RED 2
  16. #define PAL_MAGENTA 3
  17.  
  18. int whichimage;
  19. int xxadjust1;
  20. int yyadjust1;
  21. int eyeseparation = 0;
  22. int glassestype = 0;
  23. int xshift1;
  24. int yshift1;
  25. int xtrans = 0;
  26. int ytrans = 0;
  27. int red_local_left;
  28. int red_local_right;
  29. int blue_local_left;
  30. int blue_local_right;
  31. int red_crop_left   = 4;
  32. int red_crop_right  = 0;
  33. int blue_crop_left  = 0;
  34. int blue_crop_right = 4;
  35. int red_bright      = 80;
  36. int blue_bright     = 100;
  37.   
  38. BYTE T_RED;
  39.  
  40. /* Bresenham's algorithm for drawing line */
  41. void cdecl draw_line (int X1, int Y1, int X2, int Y2, int color)
  42.  
  43. {               /* uses Bresenham algorithm to draw a line */
  44.     int dX, dY;                     /* vector components */
  45.     int row, col,
  46.         final,                      /* final row or column number */
  47.         G,                  /* used to test for new row or column */
  48.         inc1,           /* G increment when row or column doesn't change */
  49.         inc2;               /* G increment when row or column changes */
  50.     char pos_slope;
  51.  
  52.     dX = X2 - X1;                   /* find vector components */
  53.     dY = Y2 - Y1;
  54.     pos_slope = (char)(dX > 0);                   /* is slope positive? */
  55.     if (dY < 0)
  56.     pos_slope = (char)!pos_slope;
  57.     if (abs (dX) > abs (dY))                /* shallow line case */
  58.     {
  59.         if (dX > 0)         /* determine start point and last column */
  60.         {
  61.             col = X1;
  62.             row = Y1;
  63.             final = X2;
  64.         }
  65.         else
  66.         {
  67.             col = X2;
  68.             row = Y2;
  69.             final = X1;
  70.         }
  71.         inc1 = 2 * abs (dY);            /* determine increments and initial G */
  72.         G = inc1 - abs (dX);
  73.         inc2 = 2 * (abs (dY) - abs (dX));
  74.         if (pos_slope)
  75.             while (col <= final)    /* step through columns checking for new row */
  76.             {
  77.                 (*plot) (col, row, color);
  78.                 col++;
  79.                 if (G >= 0)             /* it's time to change rows */
  80.                 {
  81.                     row++;      /* positive slope so increment through the rows */
  82.                     G += inc2;
  83.                 }
  84.                 else                        /* stay at the same row */
  85.                     G += inc1;
  86.             }
  87.         else
  88.             while (col <= final)    /* step through columns checking for new row */
  89.             {
  90.                 (*plot) (col, row, color);
  91.                 col++;
  92.                 if (G > 0)              /* it's time to change rows */
  93.                 {
  94.                     row--;      /* negative slope so decrement through the rows */
  95.                     G += inc2;
  96.                 }
  97.                 else                        /* stay at the same row */
  98.                     G += inc1;
  99.             }
  100.     }   /* if |dX| > |dY| */
  101.     else                            /* steep line case */
  102.     {
  103.         if (dY > 0)             /* determine start point and last row */
  104.         {
  105.             col = X1;
  106.             row = Y1;
  107.             final = Y2;
  108.         }
  109.         else
  110.         {
  111.             col = X2;
  112.             row = Y2;
  113.             final = Y1;
  114.         }
  115.         inc1 = 2 * abs (dX);            /* determine increments and initial G */
  116.         G = inc1 - abs (dY);
  117.         inc2 = 2 * (abs (dX) - abs (dY));
  118.         if (pos_slope)
  119.             while (row <= final)    /* step through rows checking for new column */
  120.             {
  121.                 (*plot) (col, row, color);
  122.                 row++;
  123.                 if (G >= 0)                 /* it's time to change columns */
  124.                 {
  125.                     col++;  /* positive slope so increment through the columns */
  126.                     G += inc2;
  127.                 }
  128.                 else                    /* stay at the same column */
  129.                     G += inc1;
  130.             }
  131.         else
  132.             while (row <= final)    /* step through rows checking for new column */
  133.             {
  134.                 (*plot) (col, row, color);
  135.                 row++;
  136.                 if (G > 0)                  /* it's time to change columns */
  137.                 {
  138.                     col--;  /* negative slope so decrement through the columns */
  139.                     G += inc2;
  140.                 }
  141.                 else                    /* stay at the same column */
  142.                     G += inc1;
  143.             }
  144.     }
  145. }   /* draw_line */
  146.  
  147. #if 0
  148. /* use this for continuous colors later */
  149. void _fastcall plot3dsuperimpose16b(int x,int y,int color)
  150. {
  151.     int tmp;
  152.     if (color != 0)         /* Keeps index 0 still 0 */
  153.     {
  154.         color = colors - color; /*  Reverses color order */
  155.         color = color / 4;
  156.         if(color == 0)
  157.             color = 1;
  158.     }
  159.     color = 3;
  160.     tmp = getcolor(x,y);
  161.  
  162.     /* map to 4 colors */
  163.     if(whichimage == 1) /* RED */
  164.     {
  165.         if(red_local_left < x && x < red_local_right)
  166.         {
  167.             putcolor(x,y,color|tmp);
  168.             if (Targa_Out)                              
  169.                 targa_color(x, y, color|tmp);
  170.         }
  171.     }
  172.     else if(whichimage == 2) /* BLUE */
  173.         if(blue_local_left < x && x < blue_local_right)
  174.         {
  175.             color = color <<2;
  176.             putcolor(x,y,color|tmp);
  177.             if (Targa_Out)                              
  178.                 targa_color(x, y, color|tmp);
  179.         }
  180. }
  181.  
  182. #endif
  183.  
  184. void _fastcall plot3dsuperimpose16(int x,int y,int color)
  185. {
  186.     int tmp;
  187.  
  188.     tmp = getcolor(x,y);
  189.  
  190.     if(whichimage == 1) /* RED */
  191.     {
  192.         color = PAL_RED;
  193.         if(tmp > 0 && tmp != color)
  194.             color = PAL_MAGENTA;
  195.         if(red_local_left < x && x < red_local_right)
  196.         {
  197.             putcolor(x,y,color);
  198.             if (Targa_Out)                              
  199.                 targa_color(x, y, color);
  200.         }
  201.     }
  202.     else if(whichimage == 2) /* BLUE */
  203.         if(blue_local_left < x && x < blue_local_right)
  204.         {
  205.             color = PAL_BLUE;
  206.             if(tmp > 0 && tmp != color)
  207.                 color = PAL_MAGENTA;
  208.             putcolor(x,y,color);
  209.             if (Targa_Out)                              
  210.                 targa_color(x, y, color);
  211.         }
  212. }
  213.  
  214.  
  215. void _fastcall plot3dsuperimpose256(int x,int y,int color)
  216. {
  217.     int tmp;
  218.     BYTE t_c;
  219.  
  220.     t_c = (BYTE)(255-color);
  221.  
  222.     if (color != 0)         /* Keeps index 0 still 0 */
  223.     {
  224.         color = colors - color; /*  Reverses color order */
  225.         if (max_colors == 236)
  226.         color = 1 + color / 21; /*  Maps colors 1-255 to 13 even ranges */
  227.         else
  228.         color = 1 + color / 18; /*  Maps colors 1-255 to 15 even ranges */
  229.     }
  230.  
  231.     tmp = getcolor(x,y);
  232.     /* map to 16 colors */
  233.     if(whichimage == 1) /* RED */
  234.     {
  235.         if(red_local_left < x && x < red_local_right)
  236.         {
  237.             /* Overwrite prev Red don't mess w/blue */
  238.             putcolor(x,y,color|(tmp&240));
  239.             if (Targa_Out)
  240.                 if (!ILLUMINE)
  241.                     targa_color(x, y, color|(tmp&240));
  242.                 else
  243.                     targa_writedisk (x+sxoffs, y+syoffs, t_c, 0, 0);
  244.         }
  245.     }
  246.     else if(whichimage == 2) /* BLUE */
  247.         if(blue_local_left < x && x < blue_local_right)
  248.         {
  249.             /* Overwrite previous blue, don't mess with existing red */
  250.             color = color <<4;
  251.             putcolor(x,y,color|(tmp&15));
  252.             if (Targa_Out)                              
  253.                 if (!ILLUMINE)
  254.                     targa_color(x, y, color|(tmp&15));
  255.                 else
  256.                 {
  257.                     targa_readdisk (x+sxoffs, y+syoffs, &T_RED, (BYTE *)&tmp, (BYTE *)&tmp);
  258.                     targa_writedisk (x+sxoffs, y+syoffs, T_RED, 0, t_c);
  259.                 }
  260.         }
  261. }
  262.  
  263. void _fastcall plotIFS3dsuperimpose256(int x,int y,int color)
  264. {
  265.     int tmp;
  266.     BYTE t_c;
  267.  
  268.     t_c = (BYTE)(255-color);
  269.  
  270.     if (color != 0)         /* Keeps index 0 still 0 */
  271.     {
  272.         /* my mind is fried - lower indices = darker colors is EASIER! */
  273.         color = colors - color; /*  Reverses color order */
  274.         if (max_colors == 236)
  275.         color = 1 + color / 21; /*  Maps colors 1-255 to 13 even ranges */
  276.         else
  277.         color = 1 + color / 18; /*  Looks weird but maps colors 1-255 to 15
  278.                     relatively even ranges */
  279.     }
  280.  
  281.     tmp = getcolor(x,y);
  282.     /* map to 16 colors */
  283.     if(whichimage == 1) /* RED */
  284.     {
  285.         if(red_local_left < x && x < red_local_right)
  286.         {
  287.             putcolor(x,y,color|tmp);
  288.             if (Targa_Out)                              
  289.                 if (!ILLUMINE)
  290.                     targa_color(x, y, color|tmp);
  291.                 else
  292.                     targa_writedisk (x+sxoffs, y+syoffs, t_c, 0, 0);
  293.         }
  294.     }
  295.     else if(whichimage == 2) /* BLUE */
  296.         if(blue_local_left < x && x < blue_local_right)
  297.         {
  298.             color = color <<4;
  299.             putcolor(x,y,color|tmp);
  300.             if (Targa_Out)                              
  301.                 if (!ILLUMINE)
  302.                     targa_color(x, y, color|tmp);
  303.                 else
  304.                 {
  305.                     targa_readdisk (x+sxoffs, y+syoffs, &T_RED, (BYTE *)&tmp, (BYTE *)&tmp);
  306.                     targa_writedisk (x+sxoffs, y+syoffs, T_RED, 0, t_c);
  307.                 }
  308.         }
  309. }
  310.  
  311. void _fastcall plot3dalternate(int x,int y,int color)
  312. {
  313.     BYTE t_c;
  314.  
  315.     t_c = (BYTE)(255-color);
  316.     /* lorez high color red/blue 3D plot function */
  317.     /* if which image = 1, compresses color to lower 128 colors */
  318.  
  319.     /* my mind is STILL fried - lower indices = darker colors is EASIER! */
  320.     color = colors - color;
  321.     if((whichimage == 1) && !((x+y)&1)) /* - lower half palette */
  322.     {
  323.         if(red_local_left < x && x < red_local_right)
  324.         {
  325.             putcolor(x,y,color>>1);
  326.             if (Targa_Out)                              
  327.                 if (!ILLUMINE)
  328.                     targa_color(x, y, color>>1);
  329.                 else
  330.                     targa_writedisk (x+sxoffs, y+syoffs, t_c, 0, 0);
  331.         }
  332.     }
  333.     else if((whichimage == 2) && ((x+y)&1) ) /* - upper half palette */
  334.     {
  335.         if(blue_local_left < x && x < blue_local_right)
  336.         {
  337.             putcolor(x,y,(color>>1)+(colors>>1));
  338.             if (Targa_Out)                              
  339.                 if (!ILLUMINE)
  340.                     targa_color(x, y, (color>>1)+(colors>>1));
  341.                 else
  342.                     targa_writedisk (x+sxoffs, y+syoffs, T_RED, 0, t_c);
  343.         }
  344.     }
  345. }
  346.  
  347. void plot_setup()
  348. {
  349.     double d_red_bright  = 0;
  350.     double d_blue_bright = 0;
  351.     int i;
  352.  
  353.     /* set funny glasses plot function */
  354.     switch(glassestype)
  355.     {
  356.     case 1:
  357.         standardplot = plot3dalternate;
  358.         break;
  359.  
  360.     case 2:
  361.         if(colors == 256)
  362.             if (fractype != IFS3D)
  363.                 standardplot = plot3dsuperimpose256;
  364.             else
  365.                 standardplot = plotIFS3dsuperimpose256;
  366.         else
  367.             standardplot = plot3dsuperimpose16;
  368.         break;
  369.  
  370.     default:
  371.         standardplot = putcolor;
  372.         break;
  373.     }
  374.  
  375.     xshift1 = xshift = (int)((XSHIFT * (double)xdots)/100);
  376.     yshift1 = yshift = (int)((YSHIFT * (double)ydots)/100);
  377.  
  378.     if(glassestype)
  379.     {
  380.         red_local_left  =   (int)((red_crop_left      * (double)xdots)/100.0);
  381.         red_local_right =   (int)(((100 - red_crop_right) * (double)xdots)/100.0);
  382.         blue_local_left =   (int)((blue_crop_left     * (double)xdots)/100.0);
  383.         blue_local_right =  (int)(((100 - blue_crop_right) * (double)xdots)/100.0);
  384.         d_red_bright    =   (double)red_bright/100.0;
  385.         d_blue_bright   =   (double)blue_bright/100.0;
  386.  
  387.         switch(whichimage)
  388.         {
  389.         case 1:
  390.             xshift  += (int)((eyeseparation* (double)xdots)/200);
  391.             xxadjust = (int)(((xtrans+xadjust)* (double)xdots)/100);
  392.             xshift1 -= (int)((eyeseparation* (double)xdots)/200);
  393.             xxadjust1 = (int)(((xtrans-xadjust)* (double)xdots)/100);
  394.             break;
  395.  
  396.         case 2:
  397.             xshift  -= (int)((eyeseparation* (double)xdots)/200);
  398.             xxadjust = (int)(((xtrans-xadjust)* (double)xdots)/100);
  399.             break;
  400.         }
  401.     }
  402.     else
  403.         xxadjust = (int)((xtrans* (double)xdots)/100);
  404.         yyadjust = (int)(-(ytrans* (double)ydots)/100);
  405.  
  406.     if (mapset)
  407.     {
  408.         ValidateLuts(MAP_name); /* read the palette file */
  409.         if(glassestype==1 || glassestype==2)
  410.         {
  411.             if(glassestype == 2 && colors < 256)
  412.             {
  413.                 dacbox[PAL_RED  ][0] = 63;
  414.                 dacbox[PAL_RED  ][1] =  0;
  415.                 dacbox[PAL_RED  ][2] =  0;
  416.  
  417.                 dacbox[PAL_BLUE ][0] =  0;
  418.                 dacbox[PAL_BLUE ][1] =  0;
  419.                 dacbox[PAL_BLUE ][2] = 63;
  420.  
  421.                 dacbox[PAL_MAGENTA][0] = 63;
  422.                 dacbox[PAL_MAGENTA][1] =    0;
  423.                 dacbox[PAL_MAGENTA][2] = 63;
  424.             }
  425.             for (i=0;i<256;i++)
  426.             {
  427.                 dacbox[i][0] = (BYTE)(dacbox[i][0] * d_red_bright);
  428.                 dacbox[i][2] = (BYTE)(dacbox[i][2] * d_blue_bright);
  429.             }
  430.         }
  431.         spindac(0,1); /* load it, but don't spin */
  432.     }
  433. }
  434.